//
//  Register_SetPasswordViewController.m
//  帮惠Mall
//
//  Created by reew on 2017/9/15.
//  Copyright © 2017年 reew. All rights reserved.
//

#import "Register_SetPasswordViewController.h"
#import "CommonUtils.h"
#import <AFNetworking.h>
#import "Login.h"
#import <SVProgressHUD.h>
#import "Account.h"
#import "System.h"
@interface Register_SetPasswordViewController ()
@property (weak, nonatomic) IBOutlet UITextField *password;
@property (weak, nonatomic) IBOutlet UITextField *ensurePassword;

@end

@implementation Register_SetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 45)];
    leftLabel.text = @"新密码";
    leftLabel.font = [UIFont systemFontOfSize:18];
    _password.leftViewMode = UITextFieldViewModeAlways;
    _password.leftView = leftLabel;
    
    UILabel *leftLabe2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 45)];
    leftLabe2.text = @"确认密码";
    leftLabe2.font = [UIFont systemFontOfSize:18];
    _ensurePassword.leftViewMode = UITextFieldViewModeAlways;
    _ensurePassword.leftView = leftLabe2;
    
    self.navigationController.navigationBarHidden = YES;
}
- (IBAction)close:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)register:(UIButton *)sender {
    [self adminRegister];
}

//管理员注册
- (void)adminRegister{
    AFHTTPSessionManager *manager = [CommonUtils getAFHTTPSessionManager];
    NSString *urlStr = [kBaseURL stringByAppendingPathComponent:kRegister_admin];
    [manager POST:urlStr parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        //参数 adminNo（会员编号），account（即手机号），passwd（密码）mobileNumber（手机验证码）
        [formData appendPartWithFormData:[[Account sharedAccount].adminNo dataUsingEncoding:NSUTF8StringEncoding] name:@"adminNo"];
        [formData appendPartWithFormData:[_phoneNum dataUsingEncoding:NSUTF8StringEncoding] name:@"account"];
        [formData appendPartWithFormData:[_password.text dataUsingEncoding:NSUTF8StringEncoding] name:@"passwd"];
        [formData appendPartWithFormData:[_authCode dataUsingEncoding:NSUTF8StringEncoding] name:@"mobileNumber"];
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
        NSString *message = responseObject[@"message"];
        NSNumber *status = responseObject[@"result"];
        [SVProgressHUD showSuccessWithStatus:message];
        if([status integerValue] == 1){
            //注册成功
            [CommonUtils saveSymbolNoWith:[Account sharedAccount].adminNo];
            [CommonUtils saveLoginfoWith:NO];
            [self showHomeView];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
        [SVProgressHUD showErrorWithStatus:@"error"];
    }];

}
- (void)saveMemberLoginfo{
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isLogin"];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isMember"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)saveAdminLoginfo{
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isLogin"];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isMember"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)showHomeView{
    UITabBarController *vc = [CommonUtils getHomeVC];
    [UIApplication sharedApplication].keyWindow.rootViewController = vc;
}
- (void)saveInfo:(NSDictionary *)responseObject{
    Account *account = [Account sharedAccount];
    
    [account saveLoginInfo:responseObject];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
